package com.rxyb.upms.service.logic;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rxyb.core.constants.DbConstants;
import com.rxyb.upms.db.bean.SysRoleMenu;
import com.rxyb.upms.db.dao.SysRoleMenuMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * @author YJH
 * @version 1.0
 * @description 根据权限查询菜单信息
 * @date 2020/6/6  13:56
 */
@Component
@Slf4j
public class SysRoleMenuLogic {

    private final SysRoleMenuMapper sysRoleMenuMapper;


    public SysRoleMenuLogic(SysRoleMenuMapper sysRoleMenuMapper) {
        this.sysRoleMenuMapper = sysRoleMenuMapper;
    }

    /**
     * 根据权限Id 查询菜单信息
     *
     * @param roleId 菜单编号
     * @return
     */
    public Optional<List<SysRoleMenu>> findRoleId(Long roleId) {
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(new QueryWrapper<SysRoleMenu>().lambda().eq(SysRoleMenu::getRoleId, roleId).eq(SysRoleMenu::getRowStatus, DbConstants.ROW_STATE_VALID));
        if (null == sysRoleMenus || sysRoleMenus.isEmpty()) {
            log.warn("根据角色编号[{}]未查询到权限信息", roleId);
            return Optional.empty();
        }
        log.warn("根据角色编号[{}]查询到权限信息[{}]", roleId, JSONUtil.toJsonStr(sysRoleMenus));
        return Optional.of(sysRoleMenus);
    }
}
